package com.lg;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.junit.Test;

public class TestStream {

	@Test
	public void test03() {
		System.out.println("使用 Java 7: ");
		// 计算空字符串
		List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
		System.out.println("列表: " + strings);
		int count = getCountEmptyStringUsingJava7(strings);
		System.out.println("java7:" + count);
		long count2 = strings.stream().filter(s -> s.isEmpty()).count();
		System.out.println("java8:" + count2);
		int count3 = getCountLength3UsingJava7(strings);
		System.out.println("java7: length3 :" + count3);
		long count4 = strings.stream().filter(s -> s.length() == 3).count();
		System.out.println("java8: length3 :" + count4);
		// 删除空字符串
		List<String> filtered = deleteEmptyStringsUsingJava7(strings);
		System.out.println("java7:" + filtered);
		List<String> collect = strings.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
		System.out.println("java8:" + collect);
		// 删除空字符串，并使用逗号把它们合并起来
		String merge = getMergedStringUsingJava7(strings, ",");
		System.out.println("java7: " + merge);
		String merge2 = strings.stream().filter(r -> !r.isEmpty()).collect(Collectors.joining(","));
		System.out.println("java8: " + merge2);
		List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
		// 获取列表元素平方数
		List<Integer> squaresList = getSquares(numbers);
		System.out.println("平方数列表: " + squaresList);
		List<Integer> collect2 = numbers.stream().map(r -> r * r).collect(Collectors.toList());
		System.out.println("平方数列表: " + collect2);
		List<Integer> integers = Arrays.asList(1, 2, 13, 4, 15, 6, 17, 8, 19);
		IntSummaryStatistics summaryStatistics = integers.stream().mapToInt(s -> s).summaryStatistics();
		int max = getMax(integers);
		System.out.println("列表: " + integers);
		System.out.println("java7 max: " + getMax(integers) + " java8 max: " + summaryStatistics.getMax());
		System.out.println("java7 Min: " + getMin(integers) + " java8 Min: " + summaryStatistics.getMin());
		System.out.println("java7 Count: " + getCount(integers) + " java8 Count: " + summaryStatistics.getCount());
		System.out.println(
				"java7 Average: " + getAverage(integers) + " java8 Average: " + summaryStatistics.getAverage());
		System.out.println("java7 Sum: " + getSum(integers) + " java8 Sum: " + summaryStatistics.getSum());
		// 输出10个随机数
		Random random = new Random();
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < 9; i++) {
			stringBuilder.append(random.nextInt()).append(" , ");
		}
		System.out.println("java7: "+stringBuilder.toString());
		new Random().ints().limit(10).sorted().forEach(System.out::print);

	}

	private double getAverage(List<Integer> integers) {
		// TODO Auto-generated method stub
		return getSum(integers) / (double) integers.size();
	}

	private int getCount(List<Integer> integers) {
		// TODO Auto-generated method stub

		return integers.size();
	}

	private int getSum(List<Integer> integers) {
		// TODO Auto-generated method stub
		int sum = 0;
		for (Integer integer : integers) {
			sum += integer;
		}
		return sum;
	}

	private int getMin(List<Integer> integers) {
		// TODO Auto-generated method stub
		int min = integers.get(0);
		for (Integer integer : integers) {
			if (integer <= min) {
				min = integer;
			}
		}
		return min;
	}

	private int getMax(List<Integer> integers) {
		// TODO Auto-generated method stub
		int max = integers.get(0);
		for (Integer integer : integers) {
			if (integer > max) {
				max = integer;
			}
		}
		return max;
	}

	private List<Integer> getSquares(List<Integer> numbers) {
		List<Integer> newList = new ArrayList<>();
		for (Integer integer : numbers) {
			newList.add(integer * integer);
		}
		return newList;
	}

	private String getMergedStringUsingJava7(List<String> strings, String split) {
		// TODO Auto-generated method stub
		StringBuilder str = new StringBuilder();
		for (String string : strings) {
			if (!string.isEmpty()) {
				if (str.length() == 0) {
					str.append(string);
				} else {
					str.append(split).append(string);
				}
			}
		}
		return str.toString();
	}

	private List<String> deleteEmptyStringsUsingJava7(List<String> strings) {
		// TODO Auto-generated method stub
		List<String> strings1 = new ArrayList<>();
		for (String string : strings) {
			if (!string.isEmpty()) {
				strings1.add(string);
			}
		}
		return strings1;
	}

	private int getCountLength3UsingJava7(List<String> strings) {
		// TODO Auto-generated method stub
		int sum = 0;
		for (String string : strings) {
			if (string.length() == 3) {
				sum++;
			}
		}
		return sum;
	}

	private int getCountEmptyStringUsingJava7(List<String> strings) {
		// TODO Auto-generated method stub
		int sum = 0;
		for (String string : strings) {
			if (string != null && string.isEmpty()) {
				sum++;
			}
		}
		return sum;
	}

	@Test
	public void test02() {
		List<String> strings = Arrays.asList("abc", "abc", "", "bc", "efg", "abcd", "", "jkl");
		String collect = strings.stream().filter(r -> !r.isEmpty()).distinct().collect(Collectors.joining(","));
		System.out.println(collect);
		ArrayList<Integer> arrayList = new ArrayList<>();
		for (int i = 0; i <= 100; i++) {
			arrayList.add(i);
		}
//		统计
//		另外，一些产生统计结果的收集器也非常有用。它们主要用于int、double、long等基本类型上，它们可以用来产生类似如下的统计结果
		IntSummaryStatistics summaryStatistics = arrayList.stream().mapToInt((s) -> s).summaryStatistics();
		System.out.println("max: " + summaryStatistics.getMax());
		System.out.println("min: " + summaryStatistics.getMin());
		System.out.println("count: " + summaryStatistics.getCount());
		System.out.println("Sum: " + summaryStatistics.getSum());
		System.out.println("Average: " + summaryStatistics.getAverage());

	}

	@Test
	public void test01() {
//		ArrayList<Integer> arrayList = new ArrayList<>();
//		for (int i = 0; i < 100; i++) {
//			arrayList.add(i);
//		}
//		List<Integer> collect = arrayList.stream().filter(s -> s%2==0).sorted((s1,s2)->s1.compareTo(s2)).collect(Collectors.toList());
//		collect.forEach(s -> System.out.println(s));
//		
//		List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//		List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
//		filtered.forEach(System.out::printf);
//		

//		map 方法用于映射每个元素到对应的结果，以下代码片段使用 map 输出了元素对应的平方数：
		List<Integer> asList = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
		List<Integer> collect2 = asList.stream().distinct().map(s -> s * s).sorted((s1, s2) -> s1.compareTo(s2))
				.collect(Collectors.toList());
		System.out.println(collect2);
//	filter 方法用于通过设置的条件过滤出元素。以下代码片段使用 filter 方法过滤出空字符串
		List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
		long count = strings.stream().filter(s -> s.isEmpty()).count();
		System.out.println(count);
//	limit 方法用于获取指定数量的流。 以下代码片段使用 limit 方法打印出 10 条数据：
		Random random = new Random();
		random.ints().limit(10).sorted().forEach(r -> System.out.println(" , " + r));
//	并行（parallel）程序
//	parallelStream 是流并行处理程序的代替方法。以下实例我们使用 parallelStream 来输出空字符串的数量：
		List<String> strings1 = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
		long count2 = strings1.parallelStream().filter(s -> s.isEmpty()).count();
		System.out.println(count2);
	}
}
